﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Cn.Cellula.Biz;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Client;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Model.System;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class List : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            GvHelper.AppendEventHandler(ref this.gvItem);
            if (!Page.IsPostBack) {
                InitForm();
                LoadEOList();
            }
                        
        }
        public string ModelName {
            get { return CodeTypeDef.Module.Item; }
        }
        private void LoadEOList() {
            String hql = " from VItemListEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and (eo.VisiableRoleLevel is null or eo.VisiableRoleKey='" + this.Power.RoleKey + "' or eo.VisiableRoleLevel<" + this.Power.RoleLevel + ") ";
            hql += " and (eo.DeptLevel is null or eo.DeptKey='" + this.Power.DeptKey + "' or eo.DeptLevel<" + this.Power.DeptLevel + ") ";
            if (!String.IsNullOrEmpty(this.hid_CategoryId.Value)) {
                if (this.hid_CategoryId.Value == TxtDef.Category.DDLValueEmpty) {
                    hql += " and (eo.CategoryName is null or eo.CategoryName ='') ";
                } else {
                    ICategoryBiz cbiz = BizFactory.Get<ICategoryBiz>();
                    IList<String> ckeys = cbiz.LoadDescendantsKeys(null, null, this.hid_CategoryId.Value);
                    if (ckeys != null && ckeys.Count > 0) {
                        hql += " and (eo.CategoryKey = '" + this.hid_CategoryId.Value + "' ";
                        for (int i = 0; i < ckeys.Count; i++) {
                            String k = ckeys[i];
                            hql += " or eo.CategoryKey='" + k + "' ";
                        }
                        hql += " ) ";
                    } else {
                        hql += " and eo.CategoryKey = '" + this.hid_CategoryId.Value + "' ";
                    }
                }
            }
            if (!String.IsNullOrEmpty(this.txt_ItemName.Text)) {
                hql += " and eo.ItemName like '%" + this.txt_ItemName.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.txt_ItemTags.Text)) {
                hql += " and eo.ItemTags like '%" + this.txt_ItemTags.Text + "%' ";
            }
            hql += " order by eo.UpdateDt desc ";
            IList<VItemListEO> list = biz.Find<VItemListEO>(hql);
            this.gvItem.DataSource = list;
            this.gvItem.Attributes[GvHelper.ATTR_TOTAL_COUNT] = (list != null) ? list.Count.ToString() : "0";
            this.gvItem.DataBind();
        }

        private void InitForm() {
            if (!String.IsNullOrEmpty(Request.QueryString["cid"])) {
                try {
                    TSysCategoryEO cat = BizFactory.Get<ICategoryBiz>().Load<TSysCategoryEO>(Request.QueryString["cid"]);
                    this.hid_CategoryId.Value = cat.CategoryId;
                    this.txt_CategoryName.Text = cat.CategoryName;
                } catch { }
            }
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            LoadEOList();
        }

        protected void btn_SuperQuery_Click(object sender, EventArgs e) {
            Response.Redirect("ItemQuery.aspx?cid=" + this.hid_CategoryId.Value);
        }
        

        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("EditItem.aspx?cid="+this.hid_CategoryId.Value);
        }

        protected void btn_EOUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvItem, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count==1) {
                Response.Redirect("EditItem.aspx?cid="+this.hid_CategoryId.Value+"&id=" + keys[0]);
            } else if(keys.Count>1){
                Response.Redirect("EditMultiItem.aspx?cid=" + this.hid_CategoryId.Value + "&idl=" + ClientBiz.ConvertList2String(keys) + "");
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_EODel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvItem, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.DeleteItem(keys);
                BizFactory.Get<ILogBiz>().LogDelete("产品", keys.Count, this.Actor, Request);
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
        protected void gvPager_OnSelectedIndexChanged(object sender, EventArgs e) {
            DropDownList ddl = (DropDownList)sender;
            this.gvItem.PageIndex = Convert.ToInt32(ddl.SelectedValue) - 1;
            LoadEOList();
        }
        protected void gvItem_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvItem.PageIndex = e.NewPageIndex;
            LoadEOList();
        }

        protected void btn_EOMultiUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvItem, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }
    }
}